package cn.ilikes.tools.nosql.jpa.persistences.contexts.operations;

import java.util.Properties;

import cn.ilikes.tools.nosql.jpa.clients.AbClient;
import cn.ilikes.tools.nosql.jpa.clients.indexs.IClientIndex;
import cn.ilikes.tools.nosql.jpa.persistences.contexts.AbstractContext;
import cn.ilikes.tools.nosql.jpa.persistences.contexts.Operation;
import cn.ilikes.tools.nosql.jpa.query.HQueryImpl.SQLQueryType;
import cn.ilikes.tools.nosql.jpa.query.iquerys.JPAQuery;
import cn.ilikes.tools.nosql.jpa.query.iquerys.IQuery.QueryType;

public class QueryOperation extends ContextOperation implements Operation<Object> {

	public JPAQuery getJpaQuery() {
		return jpaQuery;
	}

	protected int firstResult = IClientIndex.INVALID;

	protected int maxResults = IClientIndex.INVALID;

	private JPAQuery jpaQuery;

	private QueryType queryType;

	private SQLQueryType sqlQueryType;

	private Properties properties;

	public QueryOperation(AbstractContext context, JPAQuery jpaQuery, SQLQueryType sqlQueryType) {
		super(OperationType.query, context);
		this.jpaQuery = jpaQuery;
		this.sqlQueryType = sqlQueryType;
	}

	@Override
	public Object exec() {

		return jpaQuery.queryResult();
	}

	@Override
	public boolean init() {
		AbClient client = getContext().getClient();
		jpaQuery.setClient(client);
		jpaQuery.setContext(getContext());
		jpaQuery.setQueryType(queryType);
		jpaQuery.setFirstResult(firstResult);
		jpaQuery.setMaxResults(maxResults);
		jpaQuery.setSqlQueryType(sqlQueryType);
		jpaQuery.setQueryProperties(properties);
		jpaQuery.init();
		return true;
	}

	public int getFirstResult() {
		return firstResult;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getMaxResults() {
		return maxResults;
	}

	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}

	public QueryType getQueryType() {
		return queryType;
	}

	public void setQueryType(QueryType queryType) {
		this.queryType = queryType;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

}
